package com.budwk.app.sqfw.services.common;

import com.alibaba.fastjson.JSONObject;
import com.budwk.app.common.config.AppException;
import com.budwk.app.sqfw.models.Sqfw_formSummary;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwCompleteflowService;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sys.services.SysLogService;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Cnd;
import org.nutz.dao.entity.annotation.Table;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @program: dfxy-community-dev
 * @description: 后台管理公共服务
 * @author: LJT
 * @create: 2024-04-23 09:38
 **/
@IocBean
@Slf4j
public class SqfwCommonForAdminService {
    @Inject
    private SqfwFormsummaryNewService sqfwFormsummaryNewService;
    @Inject
    private SqfwCompleteflowService sqfwCompleteflowService;
    @Inject
    private SysLogService sysLogService;

    @Aop(TransAop.READ_COMMITTED)
    public int batchDelete(List<String> ids) {
        if (ids == null || ids.size() == 0) {
            throw new AppException(ResultCode.NULL_DATA_ERROR.getMsg());
        }
        // 查询数据
        Cnd cnd = Cnd.NEW();
        cnd.and(Sqfw_formSummary::getId, "in", ids);
        List<Sqfw_formSummary_new> newList = sqfwFormsummaryNewService.query(cnd);
        if (newList.isEmpty()) {
            throw new AppException(ResultCode.NULL_DATA_ERROR.getMsg());
        }
        String userId = SecurityUtil.getUserId();
        // 异步执行删除操作
        if (!newList.isEmpty()) {
            ExecutorService executorService = Executors.newSingleThreadExecutor();
            try {
                CompletableFuture.runAsync(() -> {
                    for (Sqfw_formSummary_new sqfwFormSummaryNew : newList) {
                        batchDeleteDataNew(userId, sqfwFormSummaryNew);
                    }
                }, executorService).join();
            } catch (Exception ex) {
                log.info(ex.getMessage());
            } finally {
                executorService.shutdown();
            }
        }
        return newList.size();
    }

    private void batchDeleteDataNew(String userId, Sqfw_formSummary_new data) {
        //1 记录日志
        sysLogService.insertDelLog(userId, JSONObject.toJSONString(data), Sqfw_formSummary_new.class.getAnnotation(Table.class).value(),
                "com.budwk.app.sqfw.services.customservice.SqfwCustomForAdminCommonServiceImpl.batchDeleteDataNew",
                "com.budwk.app.sqfw.controllers.admin.SqfwCustomForAdminCommonServiceImpl.batchDelete");
        // 2 删除数据
        sqfwCompleteflowService.deleteAndUpdateWO(data.getSqfwFlowId());
    }
}
